<template>
  <div class="noticeAdd">
    <div class="btns">
      <div>发送通知</div>
      <div>
        <el-button icon="CircleClose" size="large" @click="doClose(false)">关闭</el-button>
        <el-button type="success" icon="Position" size="large" @click="doSend">发送</el-button>
      </div>
    </div>
    <div class="content">
      <el-card shadow="never">
        <template #header>
          <el-icon color="#ffffff"><Bell /></el-icon>
          <span class="title">通知信息</span>
        </template>
        <template #default>
          <el-form :model="form" :rules="rules" label-width="85px" size="large" inline>
            <el-form-item label="通知名称:" prop="name">
              {{ form.name }}
            </el-form-item>
            <el-form-item label="发送方式:" prop="sendType">
              {{ form.sendType }}
            </el-form-item>
            <el-form-item label="通知分类:" prop="category">
              {{ form.category }}
            </el-form-item>
            <el-form-item label="是否启用:" prop="status">
              {{ form.status }}
            </el-form-item>
          </el-form>
        </template>
      </el-card>
      <el-card shadow="never">
        <template #header>
          <el-icon color="#ffffff"><Position /></el-icon>
          <span class="title">发送内容</span>
        </template>
        <template #default>
          <el-form :model="form" label-width="90px" size="large">
            <el-form-item label="发送模板:" prop="tempId">
              {{ form.tempId }}
            </el-form-item>
            <el-form-item v-if="form.tempId" label="标题:" prop="title">
              <span>{{ form.title }}</span>
            </el-form-item>
            <el-form-item v-if="form.tempId" label="内容:" prop="content" style="width: 50%;">
              <el-input v-model="form.content" type="textarea" :rows="4" readonly></el-input>
            </el-form-item>
          </el-form>
        </template>
      </el-card>
      <el-card v-if="form.tempId" shadow="never">
        <template #header>
          <el-icon color="#ffffff"><Document /></el-icon>
          <span class="title">内容参数</span>
        </template>
        <template #default>
          <el-table :data="form.dataList" border>
            <el-table-column label="序号" type="index" align="center" width="60"/>
            <el-table-column label="参数名称" prop="name" align="center"></el-table-column>
            <el-table-column label="参数说明" prop="desc" align="center"></el-table-column>
            <el-table-column label="具体内容" prop="value" align="center">
              <template #default="{row}">
                <el-input v-model="row.value" placeholder="请输入" clearable></el-input>
              </template>
            </el-table-column>
          </el-table>
        </template>
      </el-card>
    </div>
  </div>
</template>

<script setup name="noticeAdd">
  import { ref, getCurrentInstance } from 'vue'

  // 获取注册的全局实列
  const currentThis = getCurrentInstance().appContext.config.globalProperties
  const formRef = ref(null)

  // 定义父组件传入的值
  const props = defineProps({
    noticeId: {
      type: String,
      default: '',
      required: true
    }
  })
  const emits = defineEmits(['close'])

  let form = ref({
    noticeId: '',
    name: '撒旦反对',
    sendType: '感到翻跟斗',
    category: '啊适当放大',
    status: '启用',
    tempId: '萨的方法',
    title: '反对士大夫但是',
    content: '您好${name}，欢迎您申请成为众创运通经销商，您的密码是：${password}',
    dataList: [
      {name: 'name', desc: '真实姓名', value:''},
      {name: 'password', desc: '访问密码', value:''},
    ]
  })

  init()
  // 编辑时候初始化数据
  function init() {
    if(!props.noticeId) {
      return
    }
    form.value.noticeId = props.noticeId
  }

  // 保存
  function doSend() {
    currentThis.$messageBox.confirm(`确认发送吗？`).then(() => {
      let loading = currentThis.$loading(`正在发送`)
      saveOrderApi(form.value).then(res => {
        loading.close()
        if(res.code === '200') {
          doClose(true)
          currentThis.$message.success(res.message)
        } else {
          currentThis.$message.error(res.message)
        }
      })
    }).catch(() => {})
  }

  // 关闭
  function doClose(e) {
    emits('close', e)
  }
</script>

<style lang="less" scoped>
.noticeAdd{
  padding: 0 80px;
  .btns{
    height: 8vh;
    display: flex;
    justify-content: space-between;
    align-items: center;
    >div{
      width: 50%;
      &:first-child{
        font-size: 25px;
        font-weight: bold;
      }
      &:last-child{
        display: flex;
        justify-content: flex-end;
      }
    }
  }
  .content{
    height: 92vh;
    overflow: hidden auto;
    :deep(.el-card){
      margin-bottom: 15px;
      .el-card__header{
        display: flex;
        align-items: center;
        background: #3BB9F1;
        .el-icon{
          margin-right: 5px;
        }
        .title{
          color: #ffffff;
        }
      }
      .el-card__body{
        .el-form{
          .el-form-item {
            width: 31%;
          }
        }
      }
    }
  }
}
</style>